package com.kly.user.task;

import cn.hutool.core.net.NetUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.common.api.Response;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.kly.service.MemberService;
import com.kly.user.constant.RedisKey;
import com.kly.user.db.dao.UserDao;
import com.kly.user.db.entity.UserEntity;
import com.kly.user.service.AsyncSendMsgService;
import com.kly.user.utils.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 评价下发
 */
@Component
public class EvaluateIssueTask {

    private static Log logger = LogFactory.getLog(EvaluateIssueTask.class);

    @Autowired
    private UserDao userDao;

    @Resource
    private MemberService memberService;

    @Resource
    private AsyncSendMsgService asyncSendMsgService;

    @Autowired
    private RedisService redisService;

    @NacosValue(value = "${task.job.executor.ip}", autoRefreshed = true)
    private String taskJobExecutor;

    private String localIP = NetUtil.getLocalhostStr();

    @Scheduled(cron = "0 0 20 ? * MON")
//    @Scheduled(fixedRate = 3600000)
    public void execute() {
        if (!taskJobExecutor.contains(localIP)) {
            logger.info("=============EvaluateIssueTask, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, taskJobExecutor);
            return;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -14);
        Date date = calendar.getTime();
        int count = userDao.getRegisterDateUserCount(String.valueOf(date.getTime()));
        if (count > 0) {
            int pageSize = (count / 100) + 1;
            for (int i = 0; i < pageSize; i++) {
                List<UserEntity> userEntityList = userDao.getRegisterDateUserLimit(String.valueOf(date.getTime()), i);
                for (UserEntity userEntity : userEntityList) {
                    if (userEntity.getAppCode().equals("sparksy")) {
                        dealCondition(userEntity);
                    }
                }
                if (userEntityList.size() != 100) {
                    break;
                }
            }
        }
    }

    private void dealCondition(UserEntity userEntity) {
        Response<Boolean> response = memberService.ifMember(userEntity.getUserCode());
        if (response.isSuccess()) {
            Boolean ifMember = response.getData();
            if (ifMember) {
                Double count = redisService.zScore(RedisKey.EVALUATE_USER_LIST, userEntity.getUserCode());
                if (count == null || count < 2) {
                    String message = "Hey there!\n" +
                            "Guess what? We're totally into you!\n" +
                            "We're on a mission to fill up the Meboo community and find more awesome folks to have a blast with!\n" +
                            "Sounds amazing, right?\n" +
                            "If you like our app, please consider leaving a positive review to attract more cool and enthusiastic individuals to join Meboo!\n" +
                            "  https://play.google.com/store/apps/details?id=com.kly.meboo&pli=1";
                    asyncSendMsgService.sendMsg(userEntity.getAppCode(), userEntity.getUserCode(), "RC:InfoNtf", message, message);
                    if (count == null) {
                        redisService.zAdd(RedisKey.EVALUATE_USER_LIST, 1, userEntity.getUserCode());
                    } else {
                        redisService.incrementScore(RedisKey.EVALUATE_USER_LIST, userEntity.getUserCode(), 1);
                    }
                }
            }
        }
    }

}
